package org.jboss.netty.example.echo;

import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBuffers;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.WriteCompletionEvent;
import org.jboss.netty.handler.timeout.IdleState;
import org.jboss.netty.handler.timeout.IdleStateAwareChannelHandler;
import org.jboss.netty.handler.timeout.IdleStateEvent;

public class Heartbeat extends IdleStateAwareChannelHandler {

	int i = 0;

	@Override
	public void channelIdle(ChannelHandlerContext ctx, IdleStateEvent e)
			throws Exception {
		// TODO Auto-generated method stub

		super.channelIdle(ctx, e);

		ChannelBuffer firstMessage = ChannelBuffers.buffer(256);

		firstMessage.writeByte((byte) 0);

		if (e.getState() == IdleState.WRITER_IDLE) {
			e.getChannel().write(firstMessage);
		}
	}

	@Override
	public void writeComplete(ChannelHandlerContext ctx, WriteCompletionEvent e)
			throws Exception {
		// TODO Auto-generated method stub
		System.out.println("heart beat " + e.getFuture().isSuccess());
		
		super.writeComplete(ctx, e);
	}
}